# -*- coding: utf-8 -*-
import python_histogram_filter.simulate as sim
import python_histogram_filter.helpers as helpers
import python_histogram_filter.localizer as localizer

def show_rounded_beliefs(beliefs):
    for row in beliefs:
        print(row)

def practice1():
    R = 'r'
    G = 'g'
    # 别眼花,这是5行5列
    grid = [
        [R, G, G, G, R],
        [G, G, R, G, R],
        [G, R, G, G, G],
        [R, R, G, R, G],
        [R, G, R, G, R],
    ]
    blur = 0.05
    p_hit = 200.0
    simulation = sim.Simulation(grid, blur, p_hit)
    # 此时显示的使模拟器的初始状态
    # simulation.show_beliefs()
    simulation.run(1)
    show_rounded_beliefs(simulation.beliefs)

def practice2():
    '''
    编写以及测试感应函数
    :return:
    '''
    def practice_sense():
        R = 'r'

        # 这种写法也是够了
        _ = 'g'

        simple_grid = [
            [_, _, _],
            [_, R, _],
            [_, _, _]
        ]

        p = 1.0 / 9
        initial_beliefs = [
            [p, p, p],
            [p, p, p],
            [p, p, p]
        ]

        observation = R

        # 在Python2中,1/11=0, 1.0/11=0.09..
        expected_beliefs_after = [
            [1. / 11, 1. / 11, 1. / 11],
            [1. / 11, 3. / 11, 1. / 11],
            [1. / 11, 1. / 11, 1. / 11]
        ]

        p_hit = 3.0
        p_miss = 1.0
        beliefs_after_sensing = localizer.sense(observation, simple_grid, initial_beliefs, p_hit, p_miss)

        if helpers.close_enough(beliefs_after_sensing, expected_beliefs_after):
            print("practices pass! Your sense function is working as expected")
            return

        elif not isinstance(beliefs_after_sensing, list):
            print("Your sense function doesn't return a list!")
            return

        elif len(beliefs_after_sensing) != len(expected_beliefs_after):
            print("Dimensionality error! Incorrect height")
            return

        elif len(beliefs_after_sensing[0]) != len(expected_beliefs_after[0]):
            print("Dimensionality Error! Incorrect width")
            return

        elif beliefs_after_sensing == initial_beliefs:
            print("Your code returns the initial beliefs.")
            return

        total_probability = 0.0
        for row in beliefs_after_sensing:
            for p in row:
                total_probability += p
        if abs(total_probability - 1.0) > 0.001:
            print("Your beliefs appear to not be normalized")
            return

        print("Something isn't quite right with your sense function")
    practice_sense()


def practice3():
    '''
    编写以及测试定位器
    :return:
    '''
    R = 'r'
    G = 'g'
    grid = [
        [R, G, G, G, R, R, R],
        [G, G, R, G, R, G, R],
        [G, R, G, G, G, G, R],
        [R, R, G, R, G, G, G],
        [R, G, R, G, R, R, R],
        [G, R, R, R, G, R, G],
        [R, R, R, G, R, G, G],
    ]
    blur = 0.01
    p_hit = 100.0
    simulation = sim.Simulation(grid, blur, p_hit)

    # 移动的次数越多,则感应到的数据越多,使用贝叶斯公式计算出来的概率
    # 就越精确,定位就越准确

    for i in range(1000):
        simulation.run(3)
    simulation.show_beliefs()


def practice4():
    '''
    :return:测试矩形空间
    '''
    R = 'r'
    G = 'g'
    grid = [
        [R, G, G, G, R, R, R],
        [G, G, R, G, R, G, R],
        [G, R, G, G, G, G, R],
        [R, R, G, R, G, G, G],
    ]
    blur = 0.001
    p_hit = 100.0
    simulation = sim.Simulation(grid, blur, p_hit)
    for i in range(10):
        simulation.run(1)
        simulation.show_beliefs()

practice1()

